package cn.jy.nvr.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.jy.nvr.datasource.DataSourceConst;
import cn.jy.nvr.datasource.DataSourceContextHolder;
import cn.jy.nvr.domain.GJZ_DZ_JWD;
import cn.jy.nvr.domain.PositiveProvider;
import cn.jy.nvr.domain.ProviderBaseInfo;
import cn.jy.nvr.domain.ProviderBodyCheck;
import cn.jy.nvr.domain.ProviderRegister;
import cn.jy.nvr.mapper.GJZ_DZ_JWDMapper;
import cn.jy.nvr.mapper.PositiveProviderMapper;
import cn.jy.nvr.mapper.ProviderBaseInfoMapper;
import cn.jy.nvr.mapper.ProviderBodyCheckMapper;
import cn.jy.nvr.mapper.ProviderRegisterMapper;
import cn.jy.nvr.service.LoadPositiveProviderQuartzJob;
import cn.jy.nvr.utils.DateTools;
import cn.jy.nvr.utils.DistanceUtils;
import cn.jy.nvr.utils.TimeUtils;
@Service("LoadPositiveProviderQuartzJob")
public class LoadPositiveProviderQuartzJobImpl implements
		LoadPositiveProviderQuartzJob {
	
	@Autowired
	    ProviderBodyCheckMapper  bodyCheckMapper;
	@Autowired
	ProviderRegisterMapper providerRegisterMapper;
	 @Autowired
	 ProviderBaseInfoMapper baseInfoMapper;
	 @Autowired 
	 GJZ_DZ_JWDMapper gjz_dz_jwdMapper;
	 @Autowired
	 PositiveProviderMapper positiveProviderMapper;
	 Logger logger=Logger.getLogger(LoadPositiveProviderQuartzJobImpl.class);
	@SuppressWarnings("unchecked")
	@Override
	public void work() {
            //拿到今年的时间列表
		List<Date> findDates = TimeUtils.findDates( new Date() , new Date());
		
		for (Date date : findDates) {
			//根据时间去拿到当天的第一条体检记录的时间
			String dateFormat = DateTools.getInstance().dateFormat(date, "yyyy-MM-dd");
			DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			ProviderBodyCheck loadFirstBodyCheck = bodyCheckMapper.loadFirstBodyCheck(dateFormat);
		    if (loadFirstBodyCheck==null||loadFirstBodyCheck.getTJJSSJ()==null) {
				continue;
			}
		    DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		    List<ProviderRegister> positiveProviderRegisterList = providerRegisterMapper.loadPositiveProviderRegister(loadFirstBodyCheck.getTJJSSJ(), dateFormat);
		    for (ProviderRegister providerRegister : positiveProviderRegisterList) {
		    	 providerRegister.getGJZID();
		    	 providerRegister.getREGTIME();
		    	 //根据浆员ID号拿到基本信息
		    	 if ( providerRegister.getGJZID()==null) {
					continue;
				}
		    	 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		    	 ProviderBaseInfo providerBaseInfo = baseInfoMapper.getProviderBaseInfoByID( providerRegister.getGJZID());
		    	 if (providerBaseInfo==null) {
		    		 continue;
				}
		    	  if (providerBaseInfo.getCARDID()==null) {
		    		  continue;
				}   
		    	 //根据地址去获取经纬度      岷县经纬度  104.05413,34.44213
		    	  DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		    	   GJZ_DZ_JWD loadGJZ_DZ_JWDByCardId = gjz_dz_jwdMapper.loadGJZ_DZ_JWDByCardId(providerBaseInfo.getCARDID());
		    	   if (loadGJZ_DZ_JWDByCardId==null) {
					continue;
				   }
		    	   if (loadGJZ_DZ_JWDByCardId.getGY()==null) {
					continue;
				   }
		    	   if (loadGJZ_DZ_JWDByCardId.getGX()==null) {
		    		   continue;
		    	   }
		    	   //计算离浆站的距离
		    	   String result=null;
		    	   try {
		    		   result=DistanceUtils.twoCitysDistance( "104.05413", "34.44213", loadGJZ_DZ_JWDByCardId.getGY(), loadGJZ_DZ_JWDByCardId.getGX());
				} catch (Exception e) {
					System.out.println("没找到距离");
					e.printStackTrace();
				}
		    	if (result!=null) {
					ObjectMapper  objectMapper =new ObjectMapper();
					try {
						Map<String,Object> m =(Map<String,Object>) objectMapper.readValue(result, Map.class);
						      List<Object> list = (List<Object>) m.get("results");  
						      Map<String,Object> resultMap = (Map<String,Object>)list.get(0);
						String  distance= (String) resultMap.get("distance");
						String  duration= (String) resultMap.get("duration");
						if (distance==null) {
							continue;
						}
						if (duration==null) {
							continue;
						}
					    PositiveProvider positiveProvider = new PositiveProvider();
					    positiveProvider.setDistance(distance);
					    positiveProvider.setJYXM(providerBaseInfo.getID());
					    positiveProvider.setPredictedTime(duration);
					    positiveProvider.setRegTime(providerRegister.getREGTIME());
					    DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
					    //先查询看是否有
					    PositiveProvider loadByPositiveProvider = positiveProviderMapper.loadByPositiveProvider(positiveProvider);
					    if (loadByPositiveProvider==null) {
					    	positiveProviderMapper.save(positiveProvider);
					    	logger.info("保存积极------"+providerRegister.getREGTIME()+"登记的"+providerBaseInfo.getCARDID()+"的浆员"+providerBaseInfo.getXM());
						}
					} catch (Exception e) {
						e.printStackTrace();
						System.out.println("json有问题");
					}
				
				}   
			}
		}
	}

}
